<template>
    <div class="newEnd" v-loading="loading">
        <div>
            <div>上新年/月份</div>
            <div>
                <el-date-picker
                    value-format="yyyy-MM"
                    v-model="setdate"
                    type="month"
                    placeholder="选择年月"
                ></el-date-picker>
                <el-button type="primary" @click="onConfirmSetDate" style="margin-left:20px">设 置</el-button>
            </div>
        </div>
        <div>
            <div>上新年/月份</div>
            <div>
                <el-date-picker
                    value-format="yyyy-MM"
                    v-model="date"
                    type="month"
                    placeholder="选择年月"
                ></el-date-picker>
                <el-button type="danger" @click="onEnd" style="margin-left:20px">结 束</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { postAdminApiSetNew_products_end, postAdminApiSetNew_products_save } from "@/api"
export default {
    data() {
        return {
            loading: false,
            date: "",
            setdate: ""
        }
    },
    methods: {
        onConfirmSetDate() {

            let setdate = this.setdate
            if (setdate) {
                this.loading = true
                let form = {
                    new_year: "",
                    new_month: ""
                }
                let date = setdate.split('-')
                form.new_year = date[0];
                if (date[1] < 10) {
                    date[1] = date[1].substring(date[1].length - 1)
                }
                form.new_month = date[1];
                postAdminApiSetNew_products_save(form).then(async res => {
                    this.$message.success("设置成功")
                    this.loading = false
                    this.dialogVisibleSetDate = false
                }).catch(err => {
                    this.loading = false
                })
            } else {
                this.$message.warning("请选择上新月份")
            }
        },
        onEnd() {
            let setdate = this.date
            if (setdate) {
                this.loading = true
                let form = {
                    new_year: "",
                    new_month: ""
                }
                let date = setdate.split('-')
                form.new_year = date[0];
                if (date[1] < 10) {
                    date[1] = date[1].substring(date[1].length - 1)
                }
                form.new_month = date[1];
                postAdminApiSetNew_products_end(form).then(res => {
                    this.$message.success("设置成功")
                    this.loading = false
                    this.date = ""
                }).catch(err => {
                    this.loading = false
                })
            } else {
                this.$message.warning("请选择上新月份")
            }
        }
    }
}
</script>
<style lang="scss" scoped>
.newEnd {
    background: #fff;
    padding: 20px;
    border-radius: 2px;
    @include boxShadow;
    > div {
        margin-bottom: 20px;
        > div:nth-child(1) {
            margin-bottom: 10px;
        }
    }
}
</style>